{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "66d337ee",
   "metadata": {},
   "source": [
    "# Querying Philadelphia Phillies with LlamaIndex and NebulaGraph"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23e63d85",
   "metadata": {},
   "source": [
    "## Installation and Configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "28c37b8b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "Usage:   \n",
      "  C:\\Users\\wglantz\\AppData\\Local\\anaconda3\\python.exe -m pip install [options] <requirement specifier> [package-index-options] ...\n",
      "  C:\\Users\\wglantz\\AppData\\Local\\anaconda3\\python.exe -m pip install [options] -r <requirements file> [package-index-options] ...\n",
      "  C:\\Users\\wglantz\\AppData\\Local\\anaconda3\\python.exe -m pip install [options] [-e] <vcs project url> ...\n",
      "  C:\\Users\\wglantz\\AppData\\Local\\anaconda3\\python.exe -m pip install [options] [-e] <local project path> ...\n",
      "  C:\\Users\\wglantz\\AppData\\Local\\anaconda3\\python.exe -m pip install [options] <archive url/path> ...\n",
      "\n",
      "no such option: --silent\n"
     ]
    }
   ],
   "source": [
    "%pip install llama_index==0.8.33 ipython-ngql nebula3-python pyvis networkx youtube_transcript_api "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "01202746",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import logging\n",
    "import sys\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-#################\"\n",
    "\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.ERROR)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1fc97cc",
   "metadata": {},
   "source": [
    "## Connect to Nebula Graph and set up a new space"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7eadf711",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Connection Pool Created\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>demo_basketballplayer</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>phillies_rag</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    Name\n",
       "0  demo_basketballplayer\n",
       "1           phillies_rag"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.environ[\"GRAPHD_HOST\"] = \"127.0.0.1\"\n",
    "os.environ[\"NEBULA_USER\"] = \"root\"\n",
    "os.environ[\"NEBULA_PASSWORD\"] = \"nebula\" \n",
    "os.environ[\"NEBULA_ADDRESS\"] = \"127.0.0.1:9669\"  \n",
    "\n",
    "%reload_ext ngql\n",
    "connection_string = f\"--address {os.environ['GRAPHD_HOST']} --port 9669 --user root --password {os.environ['NEBULA_PASSWORD']}\"\n",
    "%ngql {connection_string}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5892a781",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%ngql CREATE SPACE IF NOT EXISTS phillies_rag(vid_type=FIXED_STRING(256), partition_num=1, replica_factor=1);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fa1742de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>demo_basketballplayer</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>phillies_rag</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    Name\n",
       "0  demo_basketballplayer\n",
       "1           phillies_rag"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%ngql SHOW SPACES;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7dfd6fa3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%ngql\n",
    "USE phillies_rag;\n",
    "CREATE TAG IF NOT EXISTS entity(name string);\n",
    "CREATE EDGE IF NOT EXISTS relationship(relationship string);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "879761b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%ngql CREATE TAG INDEX IF NOT EXISTS entity_index ON entity(name(256));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9275b914",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.storage.storage_context import StorageContext\n",
    "from llama_index.graph_stores import NebulaGraphStore\n",
    "\n",
    "space_name = \"phillies_rag\"\n",
    "edge_types, rel_prop_names = [\"relationship\"], [\"relationship\"]\n",
    "tags = [\"entity\"]\n",
    "\n",
    "graph_store = NebulaGraphStore(\n",
    "    space_name=space_name,\n",
    "    edge_types=edge_types,\n",
    "    rel_prop_names=rel_prop_names,\n",
    "    tags=tags,\n",
    ")\n",
    "storage_context = StorageContext.from_defaults(graph_store=graph_store)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "208852a5",
   "metadata": {},
   "source": [
    "## Load data and create KG index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3d650799",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index import (\n",
    "    LLMPredictor,\n",
    "    ServiceContext,\n",
    "    KnowledgeGraphIndex,\n",
    ")\n",
    "from llama_index.graph_stores import SimpleGraphStore\n",
    "from llama_index import download_loader\n",
    "from llama_index.llms import OpenAI\n",
    "\n",
    "# define LLM\n",
    "llm = OpenAI(temperature=0.1, model=\"gpt-3.5-turbo\")\n",
    "service_context = ServiceContext.from_defaults(llm=llm, chunk_size=512)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cb727e26",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index import load_index_from_storage\n",
    "from llama_hub.youtube_transcript import YoutubeTranscriptReader\n",
    "\n",
    "try:\n",
    "\n",
    "    storage_context = StorageContext.from_defaults(persist_dir='./storage_graph', graph_store=graph_store)\n",
    "    kg_index = load_index_from_storage(\n",
    "        storage_context=storage_context,\n",
    "        service_context=service_context,\n",
    "        max_triplets_per_chunk=15,\n",
    "        space_name=space_name,\n",
    "        edge_types=edge_types,\n",
    "        rel_prop_names=rel_prop_names,\n",
    "        tags=tags,\n",
    "        verbose=True,\n",
    "    )\n",
    "    index_loaded = True\n",
    "except:\n",
    "    index_loaded = False\n",
    "\n",
    "if not index_loaded:\n",
    "    \n",
    "    WikipediaReader = download_loader(\"WikipediaReader\")\n",
    "    loader = WikipediaReader()\n",
    "    wiki_documents = loader.load_data(pages=['Philadelphia Phillies'], auto_suggest=False)\n",
    "    print(f'Loaded {len(wiki_documents)} documents')\n",
    "\n",
    "    youtube_loader = YoutubeTranscriptReader()\n",
    "    youtube_documents = youtube_loader.load_data(ytlinks=['https://www.youtube.com/watch?v=k-HTQ8T7oVw'])    \n",
    "    print(f'Loaded {len(youtube_documents)} YouTube documents')\n",
    "\n",
    "    kg_index = KnowledgeGraphIndex.from_documents(\n",
    "        documents=wiki_documents + youtube_documents,\n",
    "        storage_context=storage_context,\n",
    "        max_triplets_per_chunk=15,\n",
    "        service_context=service_context,\n",
    "        space_name=space_name,\n",
    "        edge_types=edge_types,\n",
    "        rel_prop_names=rel_prop_names,\n",
    "        tags=tags,\n",
    "        include_embeddings=True,\n",
    "    )\n",
    "    \n",
    "    kg_index.storage_context.persist(persist_dir='./storage_graph')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6769d53",
   "metadata": {},
   "source": [
    "## Query with Text2Cypher"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1e1d23f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.query_engine import KnowledgeGraphQueryEngine\n",
    "from llama_index.storage.storage_context import StorageContext\n",
    "from llama_index.graph_stores import NebulaGraphStore\n",
    "from IPython.display import Markdown, display\n",
    "\n",
    "# query_engine = KnowledgeGraphQueryEngine(\n",
    "#     storage_context=storage_context,\n",
    "#     service_context=service_context,\n",
    "#     llm=llm,\n",
    "#     verbose=True,\n",
    "# )\n",
    "\n",
    "hybrid_query_engine = kg_index.as_query_engine(\n",
    "    include_text=True,\n",
    "    response_mode=\"tree_summarize\",\n",
    "    embedding_mode=\"hybrid\",\n",
    "    similarity_top_k=3,\n",
    "    explore_global_knowledge=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c8410735",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>Bryce Harper is a baseball player who was named MVP of the NLCS. He also fueled tensions after signing a 13-year $330 million contract.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "hybrid_query_engine = kg_index.as_query_engine()\n",
    "response = hybrid_query_engine.query(\"Tell me about Bryce Harper.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "de7326cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>Bryce Harper is a baseball player who was named MVP of the NLCS. He also fueled tensions after signing a 13-year $330 million contract.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query_engine = kg_index.as_query_engine()\n",
    "response = query_engine.query(\"Tell me about Bryce Harper.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "09923bdf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>Bryce Harper is a baseball player who was named MVP of the NLCS. He also fueled tensions after signing a 13-year $330 million contract.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = query_engine.query(\"Tell me about Bryce Harper.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b4b199aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p</th>\n",
       "      <th>e</th>\n",
       "      <th>m</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(\"Bryce Harper\" :entity{name: \"Bryce Harper\"})</td>\n",
       "      <td>(\"Bryce Harper\")-[:relationship@45106556397841...</td>\n",
       "      <td>(\"13-year $330 million contract\" :entity{name:...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(\"Bryce Harper\" :entity{name: \"Bryce Harper\"})</td>\n",
       "      <td>(\"Bryce Harper\")-[:relationship@59824214907797...</td>\n",
       "      <td>(\"NLCS\" :entity{name: \"NLCS\"})</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                p  \\\n",
       "0  (\"Bryce Harper\" :entity{name: \"Bryce Harper\"})   \n",
       "1  (\"Bryce Harper\" :entity{name: \"Bryce Harper\"})   \n",
       "\n",
       "                                                   e  \\\n",
       "0  (\"Bryce Harper\")-[:relationship@45106556397841...   \n",
       "1  (\"Bryce Harper\")-[:relationship@59824214907797...   \n",
       "\n",
       "                                                   m  \n",
       "0  (\"13-year $330 million contract\" :entity{name:...  \n",
       "1                     (\"NLCS\" :entity{name: \"NLCS\"})  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%ngql\n",
    "MATCH (p:`entity`)-[e:relationship]->(m:`entity`)\n",
    "  WHERE p.`entity`.`name` == 'Bryce Harper'\n",
    "RETURN p, e, m;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "904a738a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "        <iframe\n",
       "            width=\"100%\"\n",
       "            height=\"500px\"\n",
       "            src=\"nebulagraph.html\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "            \n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x2709497f490>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<class 'pyvis.network.Network'> |N|=3 |E|=2"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%ng_draw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "37d83dc0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The impact of the standing ovation Trey Turner received had a positive effect on his season. Following the standing ovation, Trey Turner appeared to be in higher spirits, more at ease, and less burdened by his own thoughts. This translated into improved performance on the field, as evidenced by an increase in his batting average, more successful hits, and a greater number of home runs. The support and admiration displayed by the Philadelphia fans appeared to have bolstered his self-assurance and aided him in overcoming his previous struggles.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = query_engine.query(\"How did the standing ovation Trey Turner received change his season?\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fd3f50b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p</th>\n",
       "      <th>r</th>\n",
       "      <th>q</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(\"Trey Turner\" :entity{name: \"Trey Turner\"})</td>\n",
       "      <td>(\"Trey Turner\")-[:relationship@-89965735714464...</td>\n",
       "      <td>(\"as quick\" :entity{name: \"as quick\"})</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(\"Trey Turner\" :entity{name: \"Trey Turner\"})</td>\n",
       "      <td>(\"Trey Turner\")-[:relationship@-89414636367958...</td>\n",
       "      <td>(\"a standing ovation\" :entity{name: \"a standin...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>(\"Trey Turner\" :entity{name: \"Trey Turner\"})</td>\n",
       "      <td>(\"Trey Turner\")-[:relationship@-89275189104623...</td>\n",
       "      <td>(\"in comments\" :entity{name: \"in comments\"})</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>(\"Trey Turner\" :entity{name: \"Trey Turner\"})</td>\n",
       "      <td>(\"Trey Turner\")-[:relationship@-86268653017792...</td>\n",
       "      <td>(\"25 hits\" :entity{name: \"25 hits\"})</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>(\"Trey Turner\" :entity{name: \"Trey Turner\"})</td>\n",
       "      <td>(\"Trey Turner\")-[:relationship@-86268653017792...</td>\n",
       "      <td>(\"5 home runs\" :entity{name: \"5 home runs\"})</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70</th>\n",
       "      <td>(\"Trey Turner\" :entity{name: \"Trey Turner\"})</td>\n",
       "      <td>(\"Trey Turner\")-[:relationship@804341681940123...</td>\n",
       "      <td>(\"ball and strike call\" :entity{name: \"ball an...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>(\"Trey Turner\" :entity{name: \"Trey Turner\"})</td>\n",
       "      <td>(\"Trey Turner\")-[:relationship@859994387799338...</td>\n",
       "      <td>(\"baseball\" :entity{name: \"baseball\"})</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>72</th>\n",
       "      <td>(\"Trey Turner\" :entity{name: \"Trey Turner\"})</td>\n",
       "      <td>(\"Trey Turner\")-[:relationship@859994387799338...</td>\n",
       "      <td>(\"fifth home run\" :entity{name: \"fifth home ru...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>73</th>\n",
       "      <td>(\"Trey Turner\" :entity{name: \"Trey Turner\"})</td>\n",
       "      <td>(\"Trey Turner\")-[:relationship@878913129174356...</td>\n",
       "      <td>(\"inside of his own head\" :entity{name: \"insid...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>74</th>\n",
       "      <td>(\"Trey Turner\" :entity{name: \"Trey Turner\"})</td>\n",
       "      <td>(\"Trey Turner\")-[:relationship@887114663913418...</td>\n",
       "      <td>(\"got higher\" :entity{name: \"got higher\"})</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>75 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               p  \\\n",
       "0   (\"Trey Turner\" :entity{name: \"Trey Turner\"})   \n",
       "1   (\"Trey Turner\" :entity{name: \"Trey Turner\"})   \n",
       "2   (\"Trey Turner\" :entity{name: \"Trey Turner\"})   \n",
       "3   (\"Trey Turner\" :entity{name: \"Trey Turner\"})   \n",
       "4   (\"Trey Turner\" :entity{name: \"Trey Turner\"})   \n",
       "..                                           ...   \n",
       "70  (\"Trey Turner\" :entity{name: \"Trey Turner\"})   \n",
       "71  (\"Trey Turner\" :entity{name: \"Trey Turner\"})   \n",
       "72  (\"Trey Turner\" :entity{name: \"Trey Turner\"})   \n",
       "73  (\"Trey Turner\" :entity{name: \"Trey Turner\"})   \n",
       "74  (\"Trey Turner\" :entity{name: \"Trey Turner\"})   \n",
       "\n",
       "                                                    r  \\\n",
       "0   (\"Trey Turner\")-[:relationship@-89965735714464...   \n",
       "1   (\"Trey Turner\")-[:relationship@-89414636367958...   \n",
       "2   (\"Trey Turner\")-[:relationship@-89275189104623...   \n",
       "3   (\"Trey Turner\")-[:relationship@-86268653017792...   \n",
       "4   (\"Trey Turner\")-[:relationship@-86268653017792...   \n",
       "..                                                ...   \n",
       "70  (\"Trey Turner\")-[:relationship@804341681940123...   \n",
       "71  (\"Trey Turner\")-[:relationship@859994387799338...   \n",
       "72  (\"Trey Turner\")-[:relationship@859994387799338...   \n",
       "73  (\"Trey Turner\")-[:relationship@878913129174356...   \n",
       "74  (\"Trey Turner\")-[:relationship@887114663913418...   \n",
       "\n",
       "                                                    q  \n",
       "0              (\"as quick\" :entity{name: \"as quick\"})  \n",
       "1   (\"a standing ovation\" :entity{name: \"a standin...  \n",
       "2        (\"in comments\" :entity{name: \"in comments\"})  \n",
       "3                (\"25 hits\" :entity{name: \"25 hits\"})  \n",
       "4        (\"5 home runs\" :entity{name: \"5 home runs\"})  \n",
       "..                                                ...  \n",
       "70  (\"ball and strike call\" :entity{name: \"ball an...  \n",
       "71             (\"baseball\" :entity{name: \"baseball\"})  \n",
       "72  (\"fifth home run\" :entity{name: \"fifth home ru...  \n",
       "73  (\"inside of his own head\" :entity{name: \"insid...  \n",
       "74         (\"got higher\" :entity{name: \"got higher\"})  \n",
       "\n",
       "[75 rows x 3 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%ngql \n",
    "MATCH (p:`entity`)-[r:`relationship`]->(q:`entity`)\n",
    "WHERE p.`entity`.`name` == 'Trey Turner' \n",
    "RETURN p, r, q;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e3c2c933",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "        <iframe\n",
       "            width=\"100%\"\n",
       "            height=\"500px\"\n",
       "            src=\"nebulagraph.html\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "            \n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x27094a01300>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<class 'pyvis.network.Network'> |N|=75 |E|=75"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%ng_draw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a9e67884",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The Philadelphia Phillies are a professional baseball team based in Philadelphia. They compete in Major League Baseball as a member of the National League East division. The team's home stadium is Citizens Bank Park, located in the South Philadelphia Sports Complex. The Phillies have a rich history, with two World Series championships (in 1980 and 2008), eight National League pennants, and 15 playoff appearances. They have played over 21,000 games since their establishment in 1883, with a record of 10,022 wins and 11,187 losses. The Phillies have had successful periods, including winning 11 division titles and making five consecutive playoff appearances from 2007 to 2011. They have also had notable players, such as Hall of Famers Richie Ashburn and Robin Roberts, as well as recent stars like Bryce Harper, who won the NL Most Valuable Player Award in 2021.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = query_engine.query(\"Tell me about some of the facts of Philadelphia Phillies.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d1144d69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p</th>\n",
       "      <th>e</th>\n",
       "      <th>m</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(\"Philadelphia Phillies\" :entity{name: \"Philad...</td>\n",
       "      <td>(\"Philadelphia Phillies\")-[:relationship@-9215...</td>\n",
       "      <td>(\"Joe Girardi\" :entity{name: \"Joe Girardi\"})</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(\"Philadelphia Phillies\" :entity{name: \"Philad...</td>\n",
       "      <td>(\"Philadelphia Phillies\")-[:relationship@-8815...</td>\n",
       "      <td>(\"Major League Baseball\" :entity{name: \"Major ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>(\"Philadelphia Phillies\" :entity{name: \"Philad...</td>\n",
       "      <td>(\"Philadelphia Phillies\")-[:relationship@-4023...</td>\n",
       "      <td>(\"15 playoff appearances\" :entity{name: \"15 pl...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>(\"Philadelphia Phillies\" :entity{name: \"Philad...</td>\n",
       "      <td>(\"Philadelphia Phillies\")-[:relationship@14545...</td>\n",
       "      <td>(\"National League\" :entity{name: \"National Lea...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>(\"Philadelphia Phillies\" :entity{name: \"Philad...</td>\n",
       "      <td>(\"Philadelphia Phillies\")-[:relationship@26691...</td>\n",
       "      <td>(\"American professional baseball team\" :entity...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>(\"Philadelphia Phillies\" :entity{name: \"Philad...</td>\n",
       "      <td>(\"Philadelphia Phillies\")-[:relationship@30666...</td>\n",
       "      <td>(\"Lehigh Valley IronPigs\" :entity{name: \"Lehig...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>(\"Philadelphia Phillies\" :entity{name: \"Philad...</td>\n",
       "      <td>(\"Philadelphia Phillies\")-[:relationship@42124...</td>\n",
       "      <td>(\"Philadelphia\" :entity{name: \"Philadelphia\"})</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>(\"Philadelphia Phillies\" :entity{name: \"Philad...</td>\n",
       "      <td>(\"Philadelphia Phillies\")-[:relationship@91846...</td>\n",
       "      <td>(\"eight National League pennants\" :entity{name...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>(\"Philadelphia Phillies\" :entity{name: \"Philad...</td>\n",
       "      <td>(\"Philadelphia Phillies\")-[:relationship@91846...</td>\n",
       "      <td>(\"two World Series championships\" :entity{name...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                   p  \\\n",
       "0  (\"Philadelphia Phillies\" :entity{name: \"Philad...   \n",
       "1  (\"Philadelphia Phillies\" :entity{name: \"Philad...   \n",
       "2  (\"Philadelphia Phillies\" :entity{name: \"Philad...   \n",
       "3  (\"Philadelphia Phillies\" :entity{name: \"Philad...   \n",
       "4  (\"Philadelphia Phillies\" :entity{name: \"Philad...   \n",
       "5  (\"Philadelphia Phillies\" :entity{name: \"Philad...   \n",
       "6  (\"Philadelphia Phillies\" :entity{name: \"Philad...   \n",
       "7  (\"Philadelphia Phillies\" :entity{name: \"Philad...   \n",
       "8  (\"Philadelphia Phillies\" :entity{name: \"Philad...   \n",
       "\n",
       "                                                   e  \\\n",
       "0  (\"Philadelphia Phillies\")-[:relationship@-9215...   \n",
       "1  (\"Philadelphia Phillies\")-[:relationship@-8815...   \n",
       "2  (\"Philadelphia Phillies\")-[:relationship@-4023...   \n",
       "3  (\"Philadelphia Phillies\")-[:relationship@14545...   \n",
       "4  (\"Philadelphia Phillies\")-[:relationship@26691...   \n",
       "5  (\"Philadelphia Phillies\")-[:relationship@30666...   \n",
       "6  (\"Philadelphia Phillies\")-[:relationship@42124...   \n",
       "7  (\"Philadelphia Phillies\")-[:relationship@91846...   \n",
       "8  (\"Philadelphia Phillies\")-[:relationship@91846...   \n",
       "\n",
       "                                                   m  \n",
       "0       (\"Joe Girardi\" :entity{name: \"Joe Girardi\"})  \n",
       "1  (\"Major League Baseball\" :entity{name: \"Major ...  \n",
       "2  (\"15 playoff appearances\" :entity{name: \"15 pl...  \n",
       "3  (\"National League\" :entity{name: \"National Lea...  \n",
       "4  (\"American professional baseball team\" :entity...  \n",
       "5  (\"Lehigh Valley IronPigs\" :entity{name: \"Lehig...  \n",
       "6     (\"Philadelphia\" :entity{name: \"Philadelphia\"})  \n",
       "7  (\"eight National League pennants\" :entity{name...  \n",
       "8  (\"two World Series championships\" :entity{name...  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%ngql \n",
    "MATCH (p:`entity`)-[e:relationship]->(m:`entity`)\n",
    "  WHERE p.`entity`.`name` == 'Philadelphia Phillies' \n",
    "RETURN p, e, m;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f564e410",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "        <iframe\n",
       "            width=\"100%\"\n",
       "            height=\"500px\"\n",
       "            src=\"nebulagraph.html\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "            \n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x27094a66d70>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<class 'pyvis.network.Network'> |N|=10 |E|=9"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%ng_draw"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "110773e3",
   "metadata": {},
   "source": [
    "## Query with vector index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3f61f5f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded 1 documents\n",
      "Loaded 1 YouTube documents\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "<b>Bryce Harper is a professional baseball player who has played for the Philadelphia Phillies since the 2019 season. He was signed to a 13-year, $330 million deal, making it one of the biggest contracts in baseball history. Harper has been a key player for the Phillies and has had notable achievements, including winning the NL Most Valuable Player Award for the 2021 season. He has been a significant contributor to the team's success, although they have not yet made the playoffs during his tenure.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from llama_index import VectorStoreIndex\n",
    "\n",
    "WikipediaReader = download_loader(\"WikipediaReader\")\n",
    "loader = WikipediaReader()\n",
    "wiki_documents = loader.load_data(pages=['Philadelphia Phillies'], auto_suggest=False)\n",
    "print(f'Loaded {len(wiki_documents)} documents')\n",
    "\n",
    "youtube_loader = YoutubeTranscriptReader()\n",
    "youtube_documents = youtube_loader.load_data(ytlinks=['https://www.youtube.com/watch?v=k-HTQ8T7oVw'])    \n",
    "print(f'Loaded {len(youtube_documents)} YouTube documents')\n",
    "\n",
    "vector_index = VectorStoreIndex.from_documents(wiki_documents + youtube_documents)\n",
    "vector_query_engine = vector_index.as_query_engine()\n",
    "\n",
    "response = vector_query_engine.query(\"Tell me about Bryce Harper.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "02a51d42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The standing ovation Trey Turner received seemed to have a positive impact on his season. After receiving the ovation, Turner had one of his best games in weeks and felt that the hit he got during that game was one of his best swings all year. In the 18 games since the standing ovation, Turner has been performing well, with a batting average of .343, an OPS over a thousand, and several hits, home runs, and RBIs. The love and support shown by the city of Philadelphia seemed to have boosted Turner's confidence and helped him relax, resulting in improved performance on the field.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = vector_query_engine.query(\"How did the standing ovation Trey Turner received change his season?\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "2b9eb77d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The Philadelphia Phillies are a professional baseball team based in Philadelphia. They compete in Major League Baseball as a member of the National League East division. The team's home stadium is Citizens Bank Park, located in the South Philadelphia Sports Complex. The Phillies have a history dating back to 1883 and have played at several stadiums in the city over the years. They have won two World Series championships, eight National League pennants, and have made 15 playoff appearances. The team has a loyal fan base and is known for its longevity. The Phillies have had both periods of success and periods of futility throughout their history. They were the last of the pre-expansion teams to win a World Series, with their first championship coming in 1980. Since the start of the Divisional Era in 1969, the Phillies have been one of the most successful teams in MLB, winning 11 division titles, eight National League pennants, and two World Series championships. The team's Triple-A affiliate is the Lehigh Valley IronPigs, and their Double-A affiliate is the Reading Fightin Phils. The Phillies' spring training facilities are located in Clearwater, Florida.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = vector_query_engine.query(\"Tell me about some of the facts of Philadelphia Phillies.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61fb291e",
   "metadata": {},
   "source": [
    "## Create CustomRetriever to combine vector index and KG index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f273884b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index import QueryBundle\n",
    "from llama_index.schema import NodeWithScore\n",
    "from llama_index.retrievers import BaseRetriever, VectorIndexRetriever, KGTableRetriever\n",
    "from typing import List\n",
    "\n",
    "class CustomRetriever(BaseRetriever):\n",
    "    \"\"\"Custom retriever that performs both Vector search and Knowledge Graph search\"\"\"\n",
    "\n",
    "    def __init__(\n",
    "        self,\n",
    "        vector_retriever: VectorIndexRetriever,\n",
    "        kg_retriever: KGTableRetriever,\n",
    "        mode: str = \"OR\",\n",
    "    ) -> None:\n",
    "        \"\"\"Init params.\"\"\"\n",
    "\n",
    "        self._vector_retriever = vector_retriever\n",
    "        self._kg_retriever = kg_retriever\n",
    "        if mode not in (\"AND\", \"OR\"):\n",
    "            raise ValueError(\"Invalid mode.\")\n",
    "        self._mode = mode\n",
    "\n",
    "    def _retrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]:\n",
    "        \"\"\"Retrieve nodes given query.\"\"\"\n",
    "\n",
    "        vector_nodes = self._vector_retriever.retrieve(query_bundle)\n",
    "        kg_nodes = self._kg_retriever.retrieve(query_bundle)\n",
    "\n",
    "        vector_ids = {n.node.node_id for n in vector_nodes}\n",
    "        kg_ids = {n.node.node_id for n in kg_nodes}\n",
    "\n",
    "        combined_dict = {n.node.node_id: n for n in vector_nodes}\n",
    "        combined_dict.update({n.node.node_id: n for n in kg_nodes})\n",
    "\n",
    "        if self._mode == \"AND\":\n",
    "            retrieve_ids = vector_ids.intersection(kg_ids)\n",
    "        else:\n",
    "            retrieve_ids = vector_ids.union(kg_ids)\n",
    "\n",
    "        retrieve_nodes = [combined_dict[rid] for rid in retrieve_ids]\n",
    "        return retrieve_nodes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f28c06f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index import get_response_synthesizer\n",
    "from llama_index.query_engine import RetrieverQueryEngine\n",
    "from llama_index.retrievers import VectorIndexRetriever, KGTableRetriever\n",
    "\n",
    "# create custom retriever\n",
    "vector_retriever = VectorIndexRetriever(index=vector_index)\n",
    "kg_retriever = KGTableRetriever(\n",
    "    index=kg_index, retriever_mode=\"keyword\", include_text=False\n",
    ")\n",
    "custom_retriever = CustomRetriever(vector_retriever, kg_retriever)\n",
    "\n",
    "# create response synthesizer\n",
    "response_synthesizer = get_response_synthesizer(\n",
    "    service_context=service_context,\n",
    "    response_mode=\"tree_summarize\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0058aba5",
   "metadata": {},
   "source": [
    "## Create 7 query engines and run queries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8c5faed8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# KG vector-based entity retrieval\n",
    "kg_query_engine = kg_index.as_query_engine()\n",
    "\n",
    "# KG keyword-based entity retrieval\n",
    "kg_keyword_query_engine = kg_index.as_query_engine(\n",
    "    # setting to false uses the raw triplets instead of adding the text from the corresponding nodes\n",
    "    include_text=False,\n",
    "    retriever_mode=\"keyword\",\n",
    "    response_mode=\"tree_summarize\",\n",
    ")\n",
    "\n",
    "# KG hybrid entity retrieval\n",
    "kg_hybrid_query_engine = kg_index.as_query_engine(\n",
    "    include_text=True,\n",
    "    response_mode=\"tree_summarize\",\n",
    "    embedding_mode=\"hybrid\",\n",
    "    similarity_top_k=3,\n",
    "    explore_global_knowledge=True,\n",
    ")\n",
    "\n",
    "# Raw vector index retrieval\n",
    "vector_query_engine = vector_index.as_query_engine()\n",
    "\n",
    "# Custom combo query engine\n",
    "custom_query_engine = RetrieverQueryEngine(\n",
    "    retriever=custom_retriever,\n",
    "    response_synthesizer=response_synthesizer,\n",
    ")\n",
    "\n",
    "# using KnowledgeGraphQueryEngine\n",
    "from llama_index.query_engine import KnowledgeGraphQueryEngine\n",
    "\n",
    "kgqe_query_engine = KnowledgeGraphQueryEngine(\n",
    "    storage_context=storage_context,\n",
    "    service_context=service_context,\n",
    "    llm=llm,\n",
    "    verbose=True,\n",
    ")\n",
    "\n",
    "# using KnowledgeGraphRAGRetriever\n",
    "from llama_index.query_engine import RetrieverQueryEngine\n",
    "from llama_index.retrievers import KnowledgeGraphRAGRetriever\n",
    "\n",
    "graph_rag_retriever = KnowledgeGraphRAGRetriever(\n",
    "    storage_context=storage_context,\n",
    "    service_context=service_context,\n",
    "    llm=llm,\n",
    "    verbose=True,\n",
    ")\n",
    "\n",
    "kg_rag_query_engine = RetrieverQueryEngine.from_args(\n",
    "    graph_rag_retriever, service_context=service_context\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "07739d4a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>Bryce Harper is a baseball player who was named MVP of the NLCS. He also fueled tensions after signing a 13-year $330 million contract.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kg_query_engine.query(\"Tell me about Bryce Harper.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "99889510",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>Bryce Harper is a person who has been involved in various relationships and events. He was named MVP of the NLCS and also fueled tensions after signing a 13-year $330 million contract.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kg_keyword_query_engine.query(\"Tell me about Bryce Harper.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6be9dd39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>Bryce Harper is a star outfielder who has made a significant impact in the world of baseball. He is known for his impressive hitting skills and has been a key player in the lineup of the Philadelphia Phillies. Harper signed a lucrative 13-year, $330 million contract with the Phillies as a free agent in 2019, which has generated some tensions. He was named the MVP of the NLCS and also won the NL Most Valuable Player Award in 2021. Harper's contributions have helped the Phillies reach the playoffs in 2022 and advance to the World Series.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kg_hybrid_query_engine.query(\"Tell me about Bryce Harper.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e066b6de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>Bryce Harper is a professional baseball player who has played for the Philadelphia Phillies since the 2019 season. He was signed to a 13-year, $330 million deal, making it one of the biggest contracts in baseball history. Harper has been a key player for the Phillies and has had notable achievements, including winning the NL Most Valuable Player Award for the 2021 season. He has been a significant contributor to the team's success, helping them reach the playoffs in the 2022 season and advancing to the World Series.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = vector_query_engine.query(\"Tell me about Bryce Harper.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "78d51472",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>Bryce Harper is a professional baseball player who has played for the Philadelphia Phillies. He joined the team in 2019 after signing a 13-year, $330 million contract, which was a significant move as he was taken away from the division rival Washington Nationals. Harper has been a key player for the Phillies and has had notable achievements during his time with the team. In 2021, he won the NL Most Valuable Player Award. Additionally, Harper played a crucial role in the Phillies' successful 2022 season, where they reached the playoffs for the first time since 2011 and advanced to the World Series. He was named the MVP of the NLCS during that postseason. Overall, Bryce Harper is a highly regarded player who has made a significant impact on the Philadelphia Phillies.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = custom_query_engine.query(\"Tell me about Bryce Harper.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "0ca895ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33;1m\u001b[1;3mGraph Store Query:\n",
      "MATCH (p:`entity`)-[r:`relationship`]->(e:`entity`)\n",
      "WHERE p.`entity`.`name` == 'Bryce Harper'\n",
      "RETURN e.`entity`.`name`\n",
      "\u001b[0m\u001b[33;1m\u001b[1;3mGraph Store Response:\n",
      "{'e.entity.name': ['13-year $330 million contract', 'NLCS']}\n",
      "\u001b[0m\u001b[32;1m\u001b[1;3mFinal Response: Bryce Harper is associated with a 13-year $330 million contract and the NLCS.\n",
      "\u001b[0m"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "<b>Bryce Harper is associated with a 13-year $330 million contract and the NLCS.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kgqe_query_engine.query(\"Tell me about Bryce Harper.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "c851231d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[32;1m\u001b[1;3mEntities processed: ['Bryce', 'Harper', 'Bryce Harper']\n",
      "\u001b[0m\u001b[32;1m\u001b[1;3mEntities processed: ['bryce harper', 'bryce', 'harper']\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3mGraph RAG context:\n",
      "The following are knowledge sequence in max depth 2 in the form of directed graph like:\n",
      "`subject -[predicate]->, object, <-[predicate_next_hop]-, object_next_hop ...` extracted based on key entities as subject:\n",
      "Bryce Harper{name: Bryce Harper} -[relationship:{relationship: was named MVP of}]-> NLCS{name: NLCS}\n",
      "Bryce Harper{name: Bryce Harper} -[relationship:{relationship: fueled tensions after signing}]-> 13-year $330 million contract{name: 13-year $330 million contract}\n",
      "\u001b[0m"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "<b>Bryce Harper is a person who has been involved in various relationships and events.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kg_rag_query_engine.query(\"Tell me about Bryce Harper.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "8f24ea88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The impact of the standing ovation Trey Turner received seemed to have a positive effect on his season. After the ovation, Trey appeared to be in higher spirits and more at ease. This change in mindset translated to his performance on the field, as he played the game with increased confidence. In the games that followed the ovation, Trey displayed impressive stats, including a batting average of .343, five home runs, and 15 RBIs. The support and encouragement from the Philadelphia fans appeared to have revitalized Trey and contributed to his success during that period.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kg_query_engine.query(\"How did the standing ovation Trey Turner received change his season?\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "9f2b9d5a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The standing ovation Trey Turner received likely had a positive impact on his season.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kg_keyword_query_engine.query(\"How did the standing ovation Trey Turner received change his season?\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e7f0ac92",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The standing ovation Trey Turner received had a positive impact on his season. After receiving the ovation, Turner seemed happier, more relaxed, and out of his own head. He started performing better in the games, with improved batting average, more hits, and more home runs. The love and support shown by the fans seemed to have boosted his confidence and helped him get out of his slump. Overall, the standing ovation played a significant role in changing Trey Turner's season for the better.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kg_hybrid_query_engine.query(\"How did the standing ovation Trey Turner received change his season?\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "9b9d54a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The standing ovation Trey Turner received seemed to have a positive impact on his season. After receiving the ovation, Turner had one of his best games in weeks and felt that the hit he got during that game was one of his best swings all year. In the 18 games since the standing ovation, Turner has been performing significantly better, with a batting average of .343, an OPS over a thousand, and multiple hits, home runs, and RBIs. The love and support shown by the Philadelphia fans seemed to have boosted Turner's confidence and helped him relax, resulting in improved performance on the field.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = vector_query_engine.query(\"How did the standing ovation Trey Turner received change his season?\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "46aeefec",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The standing ovation Trey Turner received had a positive impact on his season. After the ovation, Turner seemed happier, more relaxed, and out of his own head. He started performing better in games, having one of his best games in weeks. He mentioned that the hit he got during that game felt like one of his best swings all year. Since the standing ovation, Turner has been batting with a higher average, hitting more home runs, and driving in more runs. The love and support shown by the city of Philadelphia seemed to have boosted his confidence and helped him get out of his slump. Overall, it appears that the standing ovation boosted Turner's confidence and motivation, leading to a positive change in his season.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = custom_query_engine.query(\"How did the standing ovation Trey Turner received change his season?\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "326e10a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33;1m\u001b[1;3mGraph Store Query:\n",
      "MATCH (p:`entity`)-[:relationship]->(e:`entity`)\n",
      "WHERE p.`entity`.`name` == 'Trey Turner' AND e.`entity`.`name` == 'season'\n",
      "SET p.`entity`.`standing_ovation` = 'changed'\n",
      "RETURN p.`entity`.`standing_ovation`, e.`entity`.`name`\n",
      "\u001b[0mERROR:llama_index.graph_stores.nebulagraph:Query failed. Query: MATCH (p:`entity`)-[:relationship]->(e:`entity`)\n",
      "WHERE p.`entity`.`name` == 'Trey Turner' AND e.`entity`.`name` == 'season'\n",
      "SET p.`entity`.`standing_ovation` = 'changed'\n",
      "RETURN p.`entity`.`standing_ovation`, e.`entity`.`name`, Param: {}Error message: Query failed. Query: MATCH (p:`entity`)-[:relationship]->(e:`entity`)\n",
      "WHERE p.`entity`.`name` == 'Trey Turner' AND e.`entity`.`name` == 'season'\n",
      "SET p.`entity`.`standing_ovation` = 'changed'\n",
      "RETURN p.`entity`.`standing_ovation`, e.`entity`.`name`, Param: {}Error message: SyntaxError: syntax error near `MAT'\n",
      "ERROR:llama_index.graph_stores.nebulagraph:Query failed. Query: MATCH (p:`entity`)-[:relationship]->(e:`entity`)\n",
      "WHERE p.`entity`.`name` == 'Trey Turner' AND e.`entity`.`name` == 'season'\n",
      "SET p.`entity`.`standing_ovation` = 'changed'\n",
      "RETURN p.`entity`.`standing_ovation`, e.`entity`.`name`, Param: {}Error message: Query failed. Query: MATCH (p:`entity`)-[:relationship]->(e:`entity`)\n",
      "WHERE p.`entity`.`name` == 'Trey Turner' AND e.`entity`.`name` == 'season'\n",
      "SET p.`entity`.`standing_ovation` = 'changed'\n",
      "RETURN p.`entity`.`standing_ovation`, e.`entity`.`name`, Param: {}Error message: SyntaxError: syntax error near `MAT'\n",
      "ERROR:llama_index.graph_stores.nebulagraph:Query failed. Query: MATCH (p:`entity`)-[:relationship]->(e:`entity`)\n",
      "WHERE p.`entity`.`name` == 'Trey Turner' AND e.`entity`.`name` == 'season'\n",
      "SET p.`entity`.`standing_ovation` = 'changed'\n",
      "RETURN p.`entity`.`standing_ovation`, e.`entity`.`name`, Param: {}Error message: Query failed. Query: MATCH (p:`entity`)-[:relationship]->(e:`entity`)\n",
      "WHERE p.`entity`.`name` == 'Trey Turner' AND e.`entity`.`name` == 'season'\n",
      "SET p.`entity`.`standing_ovation` = 'changed'\n",
      "RETURN p.`entity`.`standing_ovation`, e.`entity`.`name`, Param: {}Error message: SyntaxError: syntax error near `MAT'\n"
     ]
    },
    {
     "ename": "RetryError",
     "evalue": "RetryError[<Future at 0x27094db1990 state=finished raised ValueError>]",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\lib\\site-packages\\tenacity\\__init__.py:382\u001b[0m, in \u001b[0;36mRetrying.__call__\u001b[1;34m(self, fn, *args, **kwargs)\u001b[0m\n\u001b[0;32m    381\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 382\u001b[0m     result \u001b[38;5;241m=\u001b[39m fn(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m    383\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m:  \u001b[38;5;66;03m# noqa: B902\u001b[39;00m\n",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\lib\\site-packages\\llama_index\\graph_stores\\nebulagraph.py:300\u001b[0m, in \u001b[0;36mNebulaGraphStore.execute\u001b[1;34m(self, query, param_map)\u001b[0m\n\u001b[0;32m    296\u001b[0m     logger\u001b[38;5;241m.\u001b[39merror(\n\u001b[0;32m    297\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mQuery failed. Query: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mquery\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, Param: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mparam_map\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    298\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mError message: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00me\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    299\u001b[0m     )\n\u001b[1;32m--> 300\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m e\n\u001b[0;32m    301\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m    302\u001b[0m     \u001b[38;5;66;03m# other exceptions\u001b[39;00m\n",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\lib\\site-packages\\llama_index\\graph_stores\\nebulagraph.py:276\u001b[0m, in \u001b[0;36mNebulaGraphStore.execute\u001b[1;34m(self, query, param_map)\u001b[0m\n\u001b[0;32m    275\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m result\u001b[38;5;241m.\u001b[39mis_succeeded():\n\u001b[1;32m--> 276\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m    277\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mQuery failed. Query: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mquery\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, Param: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mparam_map\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    278\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mError message: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mresult\u001b[38;5;241m.\u001b[39merror_msg()\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    279\u001b[0m     )\n\u001b[0;32m    280\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n",
      "\u001b[1;31mValueError\u001b[0m: Query failed. Query: MATCH (p:`entity`)-[:relationship]->(e:`entity`)\nWHERE p.`entity`.`name` == 'Trey Turner' AND e.`entity`.`name` == 'season'\nSET p.`entity`.`standing_ovation` = 'changed'\nRETURN p.`entity`.`standing_ovation`, e.`entity`.`name`, Param: {}Error message: SyntaxError: syntax error near `MAT'",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mRetryError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[41], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43mkgqe_query_engine\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mquery\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mHow did the standing ovation Trey Turner received change his season?\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m      2\u001b[0m display(Markdown(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m<b>\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mresponse\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m</b>\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\lib\\site-packages\\llama_index\\indices\\query\\base.py:23\u001b[0m, in \u001b[0;36mBaseQueryEngine.query\u001b[1;34m(self, str_or_query_bundle)\u001b[0m\n\u001b[0;32m     21\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(str_or_query_bundle, \u001b[38;5;28mstr\u001b[39m):\n\u001b[0;32m     22\u001b[0m     str_or_query_bundle \u001b[38;5;241m=\u001b[39m QueryBundle(str_or_query_bundle)\n\u001b[1;32m---> 23\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_query\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstr_or_query_bundle\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     24\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m response\n",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\lib\\site-packages\\llama_index\\query_engine\\knowledge_graph_query_engine.py:235\u001b[0m, in \u001b[0;36mKnowledgeGraphQueryEngine._query\u001b[1;34m(self, query_bundle)\u001b[0m\n\u001b[0;32m    231\u001b[0m \u001b[38;5;124;03m\"\"\"Query the graph store.\"\"\"\u001b[39;00m\n\u001b[0;32m    232\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcallback_manager\u001b[38;5;241m.\u001b[39mevent(\n\u001b[0;32m    233\u001b[0m     CBEventType\u001b[38;5;241m.\u001b[39mQUERY, payload\u001b[38;5;241m=\u001b[39m{EventPayload\u001b[38;5;241m.\u001b[39mQUERY_STR: query_bundle\u001b[38;5;241m.\u001b[39mquery_str}\n\u001b[0;32m    234\u001b[0m ) \u001b[38;5;28;01mas\u001b[39;00m query_event:\n\u001b[1;32m--> 235\u001b[0m     nodes: List[NodeWithScore] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_retrieve\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquery_bundle\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    237\u001b[0m     response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_response_synthesizer\u001b[38;5;241m.\u001b[39msynthesize(\n\u001b[0;32m    238\u001b[0m         query\u001b[38;5;241m=\u001b[39mquery_bundle,\n\u001b[0;32m    239\u001b[0m         nodes\u001b[38;5;241m=\u001b[39mnodes,\n\u001b[0;32m    240\u001b[0m     )\n\u001b[0;32m    242\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_verbose:\n",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\lib\\site-packages\\llama_index\\query_engine\\knowledge_graph_query_engine.py:200\u001b[0m, in \u001b[0;36mKnowledgeGraphQueryEngine._retrieve\u001b[1;34m(self, query_bundle)\u001b[0m\n\u001b[0;32m    193\u001b[0m logger\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mGraph Store Query:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00mgraph_store_query\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    195\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcallback_manager\u001b[38;5;241m.\u001b[39mevent(\n\u001b[0;32m    196\u001b[0m     CBEventType\u001b[38;5;241m.\u001b[39mRETRIEVE,\n\u001b[0;32m    197\u001b[0m     payload\u001b[38;5;241m=\u001b[39m{EventPayload\u001b[38;5;241m.\u001b[39mQUERY_STR: graph_store_query},\n\u001b[0;32m    198\u001b[0m ) \u001b[38;5;28;01mas\u001b[39;00m retrieve_event:\n\u001b[0;32m    199\u001b[0m     \u001b[38;5;66;03m# Get the graph store response\u001b[39;00m\n\u001b[1;32m--> 200\u001b[0m     graph_store_response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgraph_store\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mquery\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquery\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mgraph_store_query\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    201\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_verbose:\n\u001b[0;32m    202\u001b[0m         print_text(\n\u001b[0;32m    203\u001b[0m             \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mGraph Store Response:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00mgraph_store_response\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m    204\u001b[0m             color\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myellow\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m    205\u001b[0m         )\n",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\lib\\site-packages\\llama_index\\graph_stores\\nebulagraph.py:671\u001b[0m, in \u001b[0;36mNebulaGraphStore.query\u001b[1;34m(self, query, param_map)\u001b[0m\n\u001b[0;32m    670\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mquery\u001b[39m(\u001b[38;5;28mself\u001b[39m, query: \u001b[38;5;28mstr\u001b[39m, param_map: Optional[Dict[\u001b[38;5;28mstr\u001b[39m, Any]] \u001b[38;5;241m=\u001b[39m {}) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Any:\n\u001b[1;32m--> 671\u001b[0m     result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquery\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparam_map\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    672\u001b[0m     columns \u001b[38;5;241m=\u001b[39m result\u001b[38;5;241m.\u001b[39mkeys()\n\u001b[0;32m    673\u001b[0m     d: Dict[\u001b[38;5;28mstr\u001b[39m, \u001b[38;5;28mlist\u001b[39m] \u001b[38;5;241m=\u001b[39m {}\n",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\lib\\site-packages\\tenacity\\__init__.py:289\u001b[0m, in \u001b[0;36mBaseRetrying.wraps.<locals>.wrapped_f\u001b[1;34m(*args, **kw)\u001b[0m\n\u001b[0;32m    287\u001b[0m \u001b[38;5;129m@functools\u001b[39m\u001b[38;5;241m.\u001b[39mwraps(f)\n\u001b[0;32m    288\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mwrapped_f\u001b[39m(\u001b[38;5;241m*\u001b[39margs: t\u001b[38;5;241m.\u001b[39mAny, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkw: t\u001b[38;5;241m.\u001b[39mAny) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m t\u001b[38;5;241m.\u001b[39mAny:\n\u001b[1;32m--> 289\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m(f, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkw)\n",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\lib\\site-packages\\tenacity\\__init__.py:379\u001b[0m, in \u001b[0;36mRetrying.__call__\u001b[1;34m(self, fn, *args, **kwargs)\u001b[0m\n\u001b[0;32m    377\u001b[0m retry_state \u001b[38;5;241m=\u001b[39m RetryCallState(retry_object\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m, fn\u001b[38;5;241m=\u001b[39mfn, args\u001b[38;5;241m=\u001b[39margs, kwargs\u001b[38;5;241m=\u001b[39mkwargs)\n\u001b[0;32m    378\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m--> 379\u001b[0m     do \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43miter\u001b[49m\u001b[43m(\u001b[49m\u001b[43mretry_state\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretry_state\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    380\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(do, DoAttempt):\n\u001b[0;32m    381\u001b[0m         \u001b[38;5;28;01mtry\u001b[39;00m:\n",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\lib\\site-packages\\tenacity\\__init__.py:326\u001b[0m, in \u001b[0;36mBaseRetrying.iter\u001b[1;34m(self, retry_state)\u001b[0m\n\u001b[0;32m    324\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mreraise:\n\u001b[0;32m    325\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m retry_exc\u001b[38;5;241m.\u001b[39mreraise()\n\u001b[1;32m--> 326\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m retry_exc \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mfut\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mexception\u001b[39;00m()\n\u001b[0;32m    328\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mwait:\n\u001b[0;32m    329\u001b[0m     sleep \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mwait(retry_state)\n",
      "\u001b[1;31mRetryError\u001b[0m: RetryError[<Future at 0x27094db1990 state=finished raised ValueError>]"
     ]
    }
   ],
   "source": [
    "response = kgqe_query_engine.query(\"How did the standing ovation Trey Turner received change his season?\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "c5f64284",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[32;1m\u001b[1;3mEntities processed: ['standing', 'Trey Turner', 'Trey', 'ovation', 'season', 'change', 'standing ovation', 'Turner']\n",
      "\u001b[0m\u001b[32;1m\u001b[1;3mEntities processed: ['standing', 'Trey Turner', 'ovation', 'Trey', 'season', 'change', 'standing ovation', 'Turner']\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3mGraph RAG context:\n",
      "The following are knowledge sequence in max depth 2 in the form of directed graph like:\n",
      "`subject -[predicate]->, object, <-[predicate_next_hop]-, object_next_hop ...` extracted based on key entities as subject:\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: has been stuck}]-> inside of his own head{name: inside of his own head}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: has been fighting}]-> his own demons{name: his own demons}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: started out}]-> as good contact hitter{name: as good contact hitter}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: bats}]-> slowed{name: slowed}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: has won}]-> World Series{name: World Series}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: figure out}]-> what is wrong with him{name: what is wrong with him}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: had}]-> two errors{name: two errors}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: hit}]-> baseball{name: baseball}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: had}]-> one of his best games{name: one of his best games}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: continues on}]-> path to success{name: path to success}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: had}]-> one home run as a philly{name: one home run as a philly}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: had}]-> more games as a philly than in the WBC{name: more games as a philly than in the WBC}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: had}]-> Grand Slam{name: Grand Slam}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: had}]-> 819 Ops{name: 819 Ops}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: had}]-> 440 on-base percentage{name: 440 on-base percentage}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: had}]-> 3.391 batting average{name: 3.391 batting average}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: had}]-> 1.483 Ops{name: 1.483 Ops}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: head}]-> far gone{name: far gone}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: sent}]-> Team USA to championship{name: Team USA to championship}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: change}]-> demand season{name: demand season}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: argued}]-> ball and strike call{name: ball and strike call}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: got ejected}]-> from the game{name: from the game}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: has been}]-> on MLB first and second team{name: on MLB first and second team}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: has been}]-> Silver Slugger{name: Silver Slugger}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: seemingly was never}]-> going to turn it around{name: going to turn it around}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: is batting}]-> a 3-4-3 batting average{name: a 3-4-3 batting average}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: went into}]-> loan Depot indoor batting cage{name: loan Depot indoor batting cage}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: was able to drive}]-> a fastball{name: a fastball}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: has been stuck inside of}]-> his own head{name: his own head}\n",
      "standing ovation{name: standing ovation} <-[relationship:{relationship: look back and say}]- Trey Turner{name: Trey Turner} -[relationship:{relationship: showing}]-> love{name: love}\n",
      "\u001b[0m"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "<b>The standing ovation Trey Turner received likely had a positive impact on his season.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kg_rag_query_engine.query(\"How did the standing ovation Trey Turner received change his season?\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "2d4a6314",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The current stadium of the Philadelphia Phillies is Citizens Bank Park. It opened in 2004 and is located in South Philadelphia. The stadium is part of the South Philadelphia Sports Complex, which also includes the Wells Fargo Center and Lincoln Financial Field. Citizens Bank Park replaced Veterans Stadium as the home of the Phillies.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kg_query_engine.query(\"Tell me some facts about the current stadium of Philadelphia Phillies.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "17189425",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The current stadium of the Philadelphia Phillies is known for its unique feature of featuring cardboard figures.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kg_keyword_query_engine.query(\"Tell me some facts about the current stadium of Philadelphia Phillies.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "a5d84c12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The current stadium of the Philadelphia Phillies is Citizens Bank Park. It has been the team's home stadium since 2004 and is located in the South Philadelphia Sports Complex.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kg_hybrid_query_engine.query(\"Tell me some facts about the current stadium of Philadelphia Phillies.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "0d15a779",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The current stadium of the Philadelphia Phillies is Citizens Bank Park, which has been their home since 2004. It is located in the South Philadelphia Sports Complex.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = vector_query_engine.query(\"Tell me some facts about the current stadium of Philadelphia Phillies.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "0e4d7c45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The current stadium of the Philadelphia Phillies is Citizens Bank Park. It has been the team's home stadium since 2004 and is located in the South Philadelphia Sports Complex. Citizens Bank Park has a restaurant built into the base of the main scoreboard named \"Harry the K's\" in honor of the late Phillies broadcaster Harry Kalas. The stadium also features a video system that was upgraded in 2011, including a new display screen in left field, which is the largest in the National League at 76 feet high and 97 feet wide.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = custom_query_engine.query(\"Tell me some facts about the current stadium of Philadelphia Phillies.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "730b26cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33;1m\u001b[1;3mGraph Store Query:\n",
      "MATCH (s:`entity`)-[:`relationship`]->(p:`entity`)\n",
      "WHERE s.`name` == 'Philadelphia Phillies'\n",
      "RETURN p.`name`\n",
      "\u001b[0m\u001b[33;1m\u001b[1;3mGraph Store Response:\n",
      "{'p.name': []}\n",
      "\u001b[0m\u001b[32;1m\u001b[1;3mFinal Response: I'm sorry, but I couldn't find any facts about the current stadium of the Philadelphia Phillies based on the given context information.\n",
      "\u001b[0m"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "<b>I'm sorry, but I couldn't find any facts about the current stadium of the Philadelphia Phillies based on the given context information.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kgqe_query_engine.query(\"Tell me some facts about the current stadium of Philadelphia Phillies.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "c72af9e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[32;1m\u001b[1;3mEntities processed: ['Philadelphia', 'stadium', 'current stadium', 'facts', 'current', 'Philadelphia Phillies', 'Phillies']\n",
      "\u001b[0m\u001b[32;1m\u001b[1;3mEntities processed: ['phillies', 'current stadium', 'Facts', 'facts', 'Phillies', 'philadelphia', 'stadium', 'Philadelphia', 'Stadium', 'current', 'Philadelphia Phillies', 'Current', 'Current stadium']\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3mGraph RAG context:\n",
      "The following are knowledge sequence in max depth 2 in the form of directed graph like:\n",
      "`subject -[predicate]->, object, <-[predicate_next_hop]-, object_next_hop ...` extracted based on key entities as subject:\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} <-[relationship:{relationship: rivalry with}]- Washington Nationals{name: Washington Nationals} -[relationship:{relationship: extends back during}]-> original tenure{name: original tenure}\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} <-[relationship:{relationship: rivalry with}]- Washington Nationals{name: Washington Nationals} -[relationship:{relationship: increased in geographic tension due to}]-> proximity to Philadelphia{name: proximity to Philadelphia}\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} -[relationship:{relationship: based in}]-> Philadelphia{name: Philadelphia} <-[relationship:{relationship: for}]- expansion National League franchise{name: expansion National League franchise}\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} -[relationship:{relationship: based in}]-> Philadelphia{name: Philadelphia} <-[relationship:{relationship: moved}]- Toronto Blue Jays{name: Toronto Blue Jays}\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} -[relationship:{relationship: based in}]-> Philadelphia{name: Philadelphia} -[relationship:{relationship: finished}]-> MLB-best record{name: MLB-best record}\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} -[relationship:{relationship: based in}]-> Philadelphia{name: Philadelphia} -[relationship:{relationship: wanted to shed}]-> image of failure{name: image of failure}\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} -[relationship:{relationship: Triple-A affiliate}]-> Lehigh Valley IronPigs{name: Lehigh Valley IronPigs} -[relationship:{relationship: play at}]-> Coca-Cola Park{name: Coca-Cola Park}\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} -[relationship:{relationship: compete in}]-> Major League Baseball{name: Major League Baseball} <-[relationship:{relationship: universally retired by}]- 42{name: 42}\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} <-[relationship:{relationship: rivalry with}]- Washington Nationals{name: Washington Nationals}\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} -[relationship:{relationship: won}]-> two World Series championships{name: two World Series championships}\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} -[relationship:{relationship: won}]-> eight National League pennants{name: eight National League pennants}\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} -[relationship:{relationship: are}]-> American professional baseball team{name: American professional baseball team}\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} -[relationship:{relationship: compete in}]-> Major League Baseball{name: Major League Baseball}\n",
      "Philadelphia Phillies{name: Philadelphia Phillies} -[relationship:{relationship: based in}]-> Philadelphia{name: Philadelphia} -[relationship:{relationship: should use}]-> blue jay's scientific name{name: blue jay's scientific name}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} <-[relationship:{relationship: trounced}]- A's{name: A's}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} <-[relationship:{relationship: never signed with}]- J. D. Drew{name: J. D. Drew}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} <-[relationship:{relationship: named}]- Baseball America{name: Baseball America}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} -[relationship:{relationship: won}]-> highest numbers of division championships{name: highest numbers of division championships}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} -[relationship:{relationship: won}]-> first pennant{name: first pennant}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} -[relationship:{relationship: won}]-> NLDS three games to one against the Milwaukee Brewers{name: NLDS three games to one against the Milwaukee Brewers}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} -[relationship:{relationship: won}]-> NL pennant{name: NL pennant}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} -[relationship:{relationship: faced}]-> Houston Astros{name: Houston Astros}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} -[relationship:{relationship: continued to contend}]-> next few years{name: next few years}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} -[relationship:{relationship: defeated}]-> Dodgers in Los Angeles{name: Dodgers in Los Angeles}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} -[relationship:{relationship: managed to end up in}]-> third place{name: third place}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} -[relationship:{relationship: finished}]-> two games out of first{name: two games out of first}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} -[relationship:{relationship: finished}]-> 13 games under .500{name: 13 games under .500}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} -[relationship:{relationship: clinched}]-> National League East for second year in a row{name: National League East for second year in a row}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} -[relationship:{relationship: removed the interim role of}]-> Thomson{name: Thomson}\n",
      "Philadelphia{name: Philadelphia} <-[relationship:{relationship: founded in}]- Phillies{name: Phillies} -[relationship:{relationship: clinched}]-> National League East{name: National League East}\n",
      "\u001b[0m"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "<b>The current stadium of the Philadelphia Phillies is not mentioned in the given context information.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = kg_rag_query_engine.query(\"Tell me some facts about the current stadium of Philadelphia Phillies.\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c565e627",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
